import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/club/req_club.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/config/enum/refresh.dart';
import 'package:flutter_changdian_football/model/club/check_message_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/event/custom_event_util.dart';
import 'package:flutter_changdian_football/utils/ext/refresher_extension.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class ClubNewsController extends BaseGetPageController {

  ClubNewsController({
    this.clubId
  });

  final String? clubId;

  List<CheckMessageModel> checkMessageList = [];

  @override
  void requestData(
    RefreshController controller, 
    {
      Refresh refresh = Refresh.first
    }
  ) async {
    var response = await ReqClub.checkMessages(
      pageNum: page,
      clubId: clubId ?? ''
    );

    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        /// 判断是否为最后一页, 如果是， over传true， 否则false
        if (response.data['data']['hasNextPage'] == false) {
          RefreshExtension.onSuccess(controller, refresh, true); 
        } else {
          RefreshExtension.onSuccess(controller, refresh, false);
        }

        /// 下拉刷新需要清除列表
        if (refresh != Refresh.down) {
          checkMessageList.clear();
          update();
        }

        for (int i = 0; i < response.data['data']['checkMessageVOList'].length; i++) {
          checkMessageList.add(CheckMessageModel.fromJson(response.data['data']['checkMessageVOList'][i]));
        }

        showSuccess(checkMessageList);
        update();
      } else if (response.data['code'] == 401) {
        RefreshExtension.onError(controller, refresh);
      } else {
        showError();  
        RefreshExtension.onError(controller, refresh);
      }
    } else {
      showError();  
      RefreshExtension.onError(controller, refresh);
    }
  }

  /// 拒绝
  checkReject(int index) async {
    try {
      EasyLoading.show();
      var resp = await ReqClub.checkReject(
        clubId: '$clubId', 
        userId: '${checkMessageList[index].userId}', 
        messageId: '${checkMessageList[index].messageId}', 
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          checkMessageList[index].status = 'REJECT';
          update();
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_CLUB_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg'] ?? '系统异常，请稍后再试');
        }
      }
    } catch (e) {
      CustomToast.showTextToast('系统异常，请稍后再试');
    } finally {
      EasyLoading.dismiss();
      Get.back();
    }
  }

  /// 通过
  checkPass(int index) async {
    try {
      EasyLoading.show();
      var resp = await ReqClub.checkPass(
        clubId: '$clubId', 
        userId: '${checkMessageList[index].userId}', 
        messageId: '${checkMessageList[index].messageId}', 
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          checkMessageList[index].status = 'PASS';
          update();
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_CLUB_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg'] ?? '系统异常，请稍后再试');
        }
      }
    } catch (e) {
      CustomToast.showTextToast('系统异常，请稍后再试');
    } finally {
      EasyLoading.dismiss();
      Get.back();
    }
  }

}